/*
* @(#)ListStaffRecordAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.staff.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.staff.bean.StaffHierarchy;
import com.dcivision.staff.dao.StaffHierarchyDAObject;
import com.dcivision.staff.dao.StaffRecordDAObject;

/**
 * <p>Class Name:       ListStaffRecordAction.java    </p>
 * <p>Description:      The list action class for ListStaffRecord.jsp</p>
 *
 *    @author           Wong Yam Lee
 *    @company          DCIVision Limited
 *    @creation date    04/08/2003
 *    @version          $Revision: 1.10.2.1 $
 */

public class ListStaffRecordAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.10.2.1 $";


  /**
   *  Constructor - Creates a new instance of ListStaffRecordAction and define the default listName.
   */
  public ListStaffRecordAction() {
    super();
    this.setListName("staffRecordList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.staff.dao.StaffRecordDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.STAFF_RECORD);
  }

  /**
   *  Method execute() - get full list data and call super method
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    SessionContainer sessionCon = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    ListStaffRecordForm staffForm = (ListStaffRecordForm)form;
    String opMode = staffForm.getOpMode();
    StaffHierarchyDAObject dao = new StaffHierarchyDAObject(sessionCon, conn);
    if (GlobalConstant.OP_MODE_LIST_RETAIN.equals(opMode)){
    	String parentID = request.getParameter("parentID");
    	String levelIndex = request.getParameter("levelNo") != null ? ""+(TextUtility.parseInteger(request.getParameter("levelNo"))-1):"";
      if("0".equals(levelIndex)){
        levelIndex = "";
      }

      /*
       * Update Code Start
       * Author			Jim.Zhou
       * Date				2006/04/27
       * Bug				#5027
       */
    	staffForm.setParentID(parentID);
    	staffForm.setLabelFieldValue(parentID);
    	staffForm.setLevelIndex(levelIndex);
    	this.getListData(mapping,staffForm,request,response);
    	request.setAttribute("fullStaffList", dao.getFullList());
      request.setAttribute(GlobalConstant.FUNCTION_CODE_KEY, this.getFunctionCode());
    	//return mapping.findForward("L");
      /*
       * Update Code End
       */
    } else {
    	request.setAttribute("fullStaffList", dao.getFullList());
    }
    return(super.execute(mapping, form, request, response));
  }


  /**
   *  Method getListData() - retrieve the appropriate item list for current page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws Exception if cannot get the list data from DAO object
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    ListStaffRecordForm listStaffRecordForm = (ListStaffRecordForm)form; 
    StaffRecordDAObject staffRecordDAObject = new StaffRecordDAObject(sessionContainer,conn);
    List resultList = new ArrayList();
    List parentHierarchyList = new ArrayList();
    //resultList = dao.getFullList(form);
    String labelFieldValue = listStaffRecordForm.getLabelFieldValue();
    String levelIndex = listStaffRecordForm.getLevelIndex();    
    String currentHierarchyId = listStaffRecordForm.getParentID();
    if(currentHierarchyId != null && currentHierarchyId != "null" && currentHierarchyId.length() > 0 ){
        parentHierarchyList = getAllParentHierarchyList(request,TextUtility.parseIntegerObj(currentHierarchyId));        
    }
    try {
      if(!Utility.isEmpty(labelFieldValue) && !Utility.isEmpty(levelIndex)) {        
        resultList = staffRecordDAObject.getStaffRecordListByLevelFieldValueNextLevelFieldNull(form,labelFieldValue,levelIndex);        
        if(Utility.isEmpty(resultList)){
          resultList = new ArrayList();    
        }
      }else{
        resultList = staffRecordDAObject.getFullList(form);        
        if(Utility.isEmpty(resultList)){
          resultList = new ArrayList();    
        }        
      }  
    } catch (ApplicationException appEx) {
      this.rollback(request);
      throw appEx;
    } catch (Exception e) {
      log.error(e, e);
      this.rollback(request);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    }finally{
      conn=null;
    }
    
    request.setAttribute("parentHierarchyList", parentHierarchyList);
    request.setAttribute(this.m_sListName, resultList);
    if(!Utility.isEmpty(currentHierarchyId)){
    request.setAttribute("parentID", currentHierarchyId);
    }
    if(!Utility.isEmpty(levelIndex)){
    request.setAttribute("levelNo", levelIndex);
    }
  }
  
  
  /**
   * get a staff hierarchy all parent List
   *
   * @CurrentHierarchyId  the id of the currentHierarchyId
   * @return  total file size
   */
  public synchronized List getAllParentHierarchyList(HttpServletRequest request,Integer currentHierarchyId) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);    
    StaffHierarchyDAObject staffHierarchyDAObject = new StaffHierarchyDAObject(sessionContainer,conn);
    List parentHierarchyList = new ArrayList();
    List tempParentHierarchyList = null;
    if (currentHierarchyId.intValue() != 0) {
      StaffHierarchy staffHierarchy = (StaffHierarchy) staffHierarchyDAObject.getObjectByID(currentHierarchyId);
      if (staffHierarchy != null) {        
        parentHierarchyList.add(staffHierarchy);
        tempParentHierarchyList = getAllParentHierarchyList(request,staffHierarchy.getParentID());
        if (!Utility.isEmpty(tempParentHierarchyList)) {
          parentHierarchyList.addAll(tempParentHierarchyList);
        }
      }
    }
    
    staffHierarchyDAObject = null;
    return parentHierarchyList;
  }  
  
  /**
   *  Method deleteListData() - delete the item(s) from listing page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws ApplicationException the deleteion failed
   */
  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    String[] basicSelectID = form.getBasicSelectedID();
    if (!Utility.isEmpty(basicSelectID) && ((ListStaffRecordForm)form).getDeleteUserRecord().equals("true")) {
      SessionContainer sessionContainer = this.getSessionContainer(request);
      Connection conn = this.getConnection(request);
      StaffRecordDAObject  dao = new StaffRecordDAObject(sessionContainer,conn);
      for (int i=0; i<basicSelectID.length; i++) {
        dao.deleteRelatedUserRecord(TextUtility.parseIntegerObj(basicSelectID[i]));
      }
    }
    super.deleteListData(mapping,form,request,response);
  }
  
}